package com.skp.plastory;

import java.text.ParsePosition;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.Locale;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

public class DbAdapter extends SQLiteOpenHelper {
	private static final String DB_NAME = "story_temp.db";

	private static final int VERSION = 1;

	private static final String ID = "_id";
	private static final String TITLE = "title";
	private static final String CONTENT = "content";
	private static final String DATE = "date";

	private static final String TABLE_NAME = "story";
	private static final String CREATE_TABLE = "CREATE TABLE " + TABLE_NAME
			+ " (" + ID + " integer primary key autoincrement, " + TITLE
			+ " text not null, " + CONTENT + " text not null, " + DATE
			+ " text not null )";

	private SQLiteDatabase db;

	public DbAdapter(Context context) {
		super(context, DB_NAME, null, VERSION);
		db = this.getWritableDatabase();
	}

	@Override
	public synchronized void close() {
		db.close();
		super.close();
	}

	@Override
	protected void finalize() throws Throwable {
		close();
		super.finalize();
	}

	@Override
	public void onCreate(SQLiteDatabase db) {
		db.execSQL(CREATE_TABLE);
	}

	@Override
	public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
		db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
		onCreate(db);
	}

	// create
	public boolean insertInfo(String meeting, String time) {
		ContentValues cv = new ContentValues();
		cv.put(TITLE, meeting);
		cv.put(CONTENT, time);
		cv.put(DATE, getTime());

		return db.insert(TABLE_NAME, null, cv) != -1;
	}

	// getCurrentTime
	public String getTime() {
		SimpleDateFormat formatter_one = new SimpleDateFormat(
				"EEE, dd MMM yyyy hh:mm:ss", Locale.ENGLISH);
		SimpleDateFormat formatter_two = new SimpleDateFormat("yyyy-MM-dd");

		String inString = "Sun, 5 Dec 1999 00:07:21";
		ParsePosition pos = new ParsePosition(0);
		Date frmTime = formatter_one.parse(inString, pos);
		String outString = formatter_two.format(frmTime);

		// System.out.println(outString);

		return outString;
	}

	// read
	public ArrayList<InfoStory> getAllInfo() {
		ArrayList<InfoStory> info = new ArrayList<InfoStory>();
		Cursor c = db.query(TABLE_NAME,
				new String[] { ID, TITLE, CONTENT, DATE }, null, null, null,
				null, ID + " DESC");

		if (c.moveToFirst()) {
			final int indexId = c.getColumnIndex(ID);
			final int indexTitle = c.getColumnIndex(TITLE);
			final int indexContent = c.getColumnIndex(CONTENT);
			final int indexDate = c.getColumnIndex(DATE);

			do {
				int id = c.getInt(indexId);
				String title = c.getString(indexTitle);
				String content = c.getString(indexContent);
				String date = c.getString(indexDate);

				info.add(new InfoStory(id, title, content, date));
			} while (c.moveToNext());
		}
		c.close();

		return info;
	}

	// update
	public boolean updateInfo(InfoStory i) {
		ContentValues cv = new ContentValues();

		cv.put(TITLE, i.getTitle());
		cv.put(CONTENT, i.getContent());
		cv.put(DATE, i.getDate());

		String[] params = new String[] { Integer.toString(i.getId()) };
		int result = db.update(TABLE_NAME, cv, ID + "=?", params);

		return result > 0;
	}

	// delete
	public boolean deleteInfo(int id) {
		String[] params = new String[] { Integer.toString(id) };
		int result = db.delete(TABLE_NAME, ID + "=?", params);

		return result > 0;
	}

	public boolean deleteAll() {
		int result = db.delete(TABLE_NAME, null, null);

		return result > 0;
	}
}
